package com.wdz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wdz.annotations.Limit;
import com.wdz.annotations.ResponseResult;
import com.wdz.category.entity.Category;
import com.wdz.http.Response;
import com.wdz.search.SearchGets;
import com.wdz.systemlogs.entity.SystemLogs;
import com.wdz.systemlogs.service.ISystemLogsService;
import net.hasor.web.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * RESTful风格
 */
@RestController
@RequestMapping("/systemlogs")
@ResponseResult
public class SystemlogsController {
    @Autowired
    private ISystemLogsService iSystemLogsService;
    @PostMapping("/")
    public Response postSystemlogs(@RequestBody SystemLogs systemLogs){
        iSystemLogsService.insert(systemLogs);
        return Response.success((Object) systemLogs.getId());
    }
    @Limit(limitNum = 2, name = "getSystemlogs")
    @GetMapping("/{pageNo}/{pageSize}")
    public Response getSystemlogs(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String,Object> data = new HashMap<>();
        data.put("total",iSystemLogsService.selectCount(null));
        data.put("data",iSystemLogsService.selectPage(new Page<>(pageNo,pageSize),new EntityWrapper<SystemLogs>().orderBy("create_date",false)).getRecords());
        return Response.success(data);
    }
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable String id){iSystemLogsService.deleteById(id); return Response.success();}
    /**
     * 批量删除
     */
    @DeleteMapping("/")
    public Response batchDelete(@RequestBody List<String> ids){iSystemLogsService.deleteBatchIds(ids); return Response.success();}

    /**
     * 高级检索
     * @return
     */
    @PostMapping("/query/{pageNo}/{pageSize}")
    public Response advancedFiltering(@RequestBody Map<String,Object> params,@PathVariable int pageNo,@PathVariable int pageSize){
        Set<String> keys = params.keySet();
        StringBuffer sql = new StringBuffer();
        String[] values = new String[params.size()+1];
        int i=0;
        for (String key : keys){
            if("time_zone".equals(key)){
                String[] time_zone = StringUtils.split(String.valueOf(params.get(key)),",");
                assert time_zone != null;
                if(!"0".equals(time_zone[0])) {
                    sql.append("create_date>=");
                    sql.append("{");
                    sql.append(i);
                    sql.append("}");
                    values[i] = time_zone[0];
                    sql.append(" and ");
                }
                if(!"0".equals(time_zone[1])) {
                    sql.append("create_date<=");
                    sql.append("{");
                    i++;
                    sql.append(i);
                    sql.append("}");
                    sql.append(" and ");
                    values[i] = time_zone[1];
                }
            }else {
                sql.append(key);
                sql.append("={");
                sql.append(i);
                sql.append("}");
                sql.append(" and ");
                values[i] = String.valueOf(params.get(key));
            }
            i++;
        }
        sql.replace(sql.length()-5,sql.length(),"");
        Map<String,Object> d = new HashMap<>();
        d.put("total",iSystemLogsService.selectCount(new EntityWrapper<SystemLogs>().where(String.valueOf(sql),values)));
        d.put("data",iSystemLogsService.selectPage(new Page<>(pageNo,pageSize),new EntityWrapper<SystemLogs>().where(String.valueOf(sql),values)));
        return Response.success(d);
    }
    @PostMapping("/gets")
    public Map<String,Object> gets(Integer pageNo, Integer pageSize, SystemLogs systemLogs, Boolean needPage){
        return SearchGets.gets(iSystemLogsService,systemLogs,pageNo,pageSize,needPage);
    }
}
